
package net.qiqbframework.messaging.annotation;


import net.qiqbframework.messaging.BatchMessage;
import net.qiqbframework.messaging.Message;
import net.qiqbframework.persisthanding.PersistMessage;

import java.util.HashMap;
import java.util.Map;


public class MapPayloadParameterResolver implements HandlerParameterResolver<Map<Object,Object>> {
    /**
     * 集合内的元素类型
     */
    private final Class<?> payloadType;

    public MapPayloadParameterResolver(Class<?> payloadType) {
        this.payloadType = payloadType;
    }

    @Override
    public Map<Object,Object> resolveParameter(Message<?> message) {

        if (message instanceof BatchMessage<?> batchMessage){
            Map<Object,Object> value = new HashMap<>();
            for (Message<?> msg : batchMessage.getPayload()) {
                if (msg instanceof PersistMessage<?> persistMessage){
                    value.put(persistMessage.getPayload(),persistMessage.getSnapshotAggregateRoot());
                }
            }

            return value;
        }
        return null;
    }

    @Override
    public boolean support(Message message) {

        return message instanceof BatchMessage<?>;
    }

    @Override
    public Class<?> supportedPayloadType() {
        return Object.class;
    }
}
